// 导入 SparkMD5 库
// self.importScripts('https://cdnjs.cloudflare.com/ajax/libs/spark-md5/3.0.0/spark-md5.min.js');
self.importScripts('./spark.js');

// 监听主线程消息
self.onmessage = async (e) => {
  const { file } = e.data;

  // 计算文件哈希
  const hash = await calculateFileHash(file);
  self.postMessage(hash);
};

// 计算文件哈希
function calculateFileHash (file) {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsArrayBuffer(file);
    reader.onload = (e) => {
      const buffer = e.target.result;
      const spark = new self.SparkMD5.ArrayBuffer();
      spark.append(buffer);
      const hash = spark.end();
      resolve(hash);
    };
  });
}